// 04myCenter/myCenter.js
var co = require('../resource/js/common.js')
var M = require('../resource/js/md5.js')
var ws = require('../resource/js/websocket.js')
var app = getApp()
var _this, _data
var interval = null //倒计时函数
Page({
   data: {
      userInfo: "",
      //切换登录方式
      isThis: 0,
      // 登录方式错误提示信息
      isUserName: '',
      isPassWord: '',
      isPhoneNumber: '',
      isVerificationCode: '',
      // 短信验证码
      time: '获取验证码',
      currentTime: 120,
      smsCode: '',
      // 输入的手机号
      phoneNumber: '',
      tempPhone: '',
      // 传递信息
      loginInfo: '',
      isShowWork:false
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      _this = this
      _data = this.data
      _this.getBizList()
      if (co.empty(app.Data.userInfo)) {
         app.Data.userInfo = wx.getStorageSync('userInfo')

      }
      if (!co.empty(app.Data.userInfo)) {
         _this.initWS_Login()
      }
      this.setData({
         userInfo: app.Data.userInfo,
         loginInfo: wx.getStorageSync('loginInfo'),
         login_phoneNumber: wx.getStorageSync('login_phoneNumber'),
      })
   },
   //跳转到废品订单
   toWasteOrder: function() {
      wx.navigateTo({
         url: '/04myCenter/04wasteOrder/wasteOrder',
      })
   },

   // 再次调用微信授权
   getWxOpenInfo: function(e) {
      //   console.log("【获取用户信息】===", e.detail)
      var session_key = wx.getStorageSync('session_key')
      if (co.empty(session_key)) {
         app.wxLogin(function(res) {
            console.log("【app.wxLogin登录结果】===", res)
            _this.decryptData(e.detail)
         })
         return
      }
      _this.decryptData(e.detail)
   },

   //解密数据
   decryptData: function(data) {
      app.decryptWxUserInfoData(data, function(res) {
         console.log("【app.decryptWxUserInfoData登录结果】===", res)
         _this.doLoginEvent(res)
      })
   },

   doLoginEvent(res) {
      app.Data.userInfo = res.data
      //console.log('res',res)
      this.setData({
         userInfo: res.data
      })
      console.log('userInfo==', _data.userInfo)
   },

   // 获取手机号
   getPhoneNumber: function(e) {
      var phone = e.detail.value //输入的手机号码存入
      this.setData({
         phoneNumber: phone
      })
   },

   // 获取验证码
   getVerCode: function() {
      var inPhone = this.data.phoneNumber //输入手机号
      var telRule = /^1[0-9]{10}$/ //手机号码格式
      var isPhoneNumber = ''
      if (inPhone == '') {
         co.toastAllText("请输入手机号码!")
      } else if (!telRule.test(inPhone)) {
         isPhoneNumber = '*手机号码格式错误*'
      } else {
         this.getCode();
         this.setData({
            disabled: true
         })
         var param = {
            mobile: this.data.phoneNumber,
            type: 'quick_login',
            appid: 1400134010
         }
         co.getShareApi('api/Sms/getPhoneCode', param, function(res) {
            console.log("【获取返回数据】===", res.data)
            _this.setData({
               smsCode: res.data.Sms_Code
            })
         })
      }
      this.setData({
         isPhoneNumber: isPhoneNumber,
         tempPhone: inPhone,
      })
   },

   // 确认登录
   confirmLogin: function(e) {
      // console.log("【form携带值为】===", e.detail.value)
      var et = e.detail.value //form携带值为
      if (co.empty(et.phoneNumber))
         wx.setStorageSync('loginInfo', et)
      else {
         wx.setStorageSync('login_phoneNumber', et.phoneNumber)
      }
      var id = this.data.isThis //登录方式id
      var smsCode = this.data.smsCode //获取到的短信验证码
      var tmPhone = this.data.tempPhone //获取验证码的手机码
      var USN = et.userName //form提交用户名
      var PSW = et.passWord //form提交密码
      var PHN = et.phoneNumber //from提交手机号
      var VCO = et.verificationCode //from提交验证码
      var isUserName = ''
      var isPassWord = ''
      var isPhoneNumber = ''
      var isVerificationCode = ''
      if (id == 0) {
         if (USN == '') {
            isUserName = '*用户名必填*'
         }
         if (PSW == '') {
            isPassWord = '*密码必填*'
         }
         var param = {
            User_Mobile: USN,
            User_Password: M.md5(PSW)
         }
         this.Login(param)
      } else {
         if (PHN == '') {
            isPhoneNumber = '*手机号码必填*'
         }
         if (VCO == '') {
            isVerificationCode = '*验证码必填*'
         }
         // 判断验证码是否相等
         if (VCO != smsCode) {
            isVerificationCode = '*验证码错误*'
         } else if (tmPhone != PHN) {
            isVerificationCode = '*验证码已过期*'
         } else {
            var param = {
               User_Mobile: PHN,
               Sms_Code: VCO
            }
            this.Login(param)
         }
      }
      this.setData({
         isUserName: isUserName,
         isPassWord: isPassWord,
         isPhoneNumber: isPhoneNumber,
         isVerificationCode: isVerificationCode
      })
   },

   // 登录
   Login: function(param) {
      var isUserName = ''
      var isPassWord = ''
      var isPhoneNumber = ''
      // 账号密码登录
      co.getShareApi('jyhs/Usermg/userLogin', param, function(res) {
         // console.log("【登录成功返回数据】===",res.data)
         if (res.code == 29300) {
            isUserName = res.msg
         }
         if (res.code == 29375) {
            isPassWord = res.msg
         }
         if (res.code == 0) {
            _this.setData({
               userInfo: res.data
            })
            app.Data.userInfo = res.data
            _this.initWS_Login()
         }
         console.log('res.data===4', res)
         _this.setData({
            isUserName: res.data.User_Name,
            isPassWord: res.data.User_Password,
            isPhoneNumber: res.data.User_Mobile
         })
         console.log("【data】===", _this.data)
         wx.setStorage({
            key: 'userInfo',
            data: _this.data.userInfo
         })
      })
   },
   // 点击切换登录
   changeContent: function(e) {
      var id = e.currentTarget.id
      if (id == '0') {
         this.setData({
            isThis: 0
         })
      } else {
         this.setData({
            isThis: 1
         })
      }
   },
   // //初始化socket
   initWS_Login() {
      var _Room_ID = 'jyhs_user';
      ws.initWebsocket(app.Data.userInfo, _Room_ID, function(res) {
         console.log('【wss初始化及登录结果】', res)
      })
   },
   /*获取短信验证码倒计时*/
   getCode: function(options) {
      var currentTime = this.data.currentTime
      interval = setInterval(function() {
         currentTime--;
         _this.setData({
            time: currentTime + '秒'
         })
         if (currentTime <= 0) {
            clearInterval(interval)
            _this.setData({
               time: '重新发送',
               currentTime: 120,
               disabled: false,
               smsCode: '',
            })
         }
      }, 1000)
   },
   //跳转到注册成为商家
   toRegister: function() {
      wx.navigateTo({
         url: '/02login/01register/register',
      })
   },
   getBizList: function () {
      var url = 'jyhs/Admin/getBizList'
      co.getShareApi(url, {
         User_ID: app.Data.userInfo.User_ID
      }, function (res) { 
         if(res.data.length > 0){
            _this.setData({
               isShowWork: true
            })
         } 
      })
   },
   //跳转到意见反馈
   feedBack: function() {
      wx.navigateTo({
         url: '/04myCenter/06feedback/feedback',
      })
   },
   //跳转到绑定手机
   toBind: function() {
      wx.navigateTo({
         url: '/04myCenter/02set/set',
      })
   },
   //跳转到钱包
   toMyChange: function() {
      wx.navigateTo({
         url: '/04myCenter/01myWallet/01myChange/myChange',
      })
   },
   //跳转到收货地址
   toRecevingAddress: function() {
      wx.navigateTo({
         url: '/04myCenter/03receiveAddress/03receiverAddress',
      })
   },
   //跳转到有员工的全部企业
   toWorker: function() {
      wx.navigateTo({
         url: '/04myCenter/07workerEnterprise/workerEnterpriseList',
      })
   },
   //跳转到企业列表
   toEnterpriseAuth: function() {
      wx.navigateTo({
         url: '/04myCenter/08bizAuth/bizAuth',
      })
   },
   //跳转到关于旧约页面
   gotoAboutJyhs() {
       wx.navigateTo({
          url: '/04myCenter/12aboutJyhs/aboutJyhs',
       })
   },
   //成为推广员
   inviteBusiness: function() {
      wx.navigateTo({
         url: '/04myCenter/09inviteBusiness/inviteBusiness',
      })
   },
   //积分
   integral: function() {
      // wx.navigateTo({
      //    url: '/04myCenter/10integral/integral',
      // })
   },
   //商城订单
   toMyShoppingOrder: function() {
      wx.navigateTo({
         url: '/04myCenter/11shoppingOrder/shoppingOrder',
      })
   },

   //跳转回收帮小程序
   intoJyhsb() {
      wx.navigateToMiniProgram({
         appId: 'wxa97a45973f5f3774',
         path: '01login/reg/regJyhsb',
         extraData: {},
         envVersion: 'release', //'develop', //'trial'
         success(res) {
            // 打开成功
         }
      })
   },
   //订单
   toOrder: function(e) {
      //console.log('e',e)
      var id = e.currentTarget.dataset.id
      //console.log('id',id)
      wx.navigateTo({
         url: '/04myCenter/11shoppingOrder/shoppingOrder?index=' + id,
      })
   }
})